package info.ask4research.web;

import info.ask4research.model.FileUploadBean;
import info.ask4research.model.UnitOfLearning;
import info.ask4research.model.activities.Activity;
import info.ask4research.model.activities.LearningActivity;
import info.ask4research.model.activities.SupportActivity;
import info.ask4research.model.enums.FinishingActivity;
import info.ask4research.model.roles.Role;
import info.ask4research.model.toolsAndMaterials.SentMailService;
import info.ask4research.service.IActivityService;
import info.ask4research.service.IRoleService;
import info.ask4research.service.impl.ActivityService;
import info.ask4research.web.dto.ActivityRolesDTO;
import info.ask4research.web.dto.ToolsAndMaterialsRolesDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class ActivityController {

	private IActivityService activityService;
	private IRoleService roleService;

	@RequestMapping(value="jsp/activityFiles/fsGrid.action")
	public @ResponseBody Map saveAddonMtCTitle(
			@RequestParam(value="title", required=false) String title,
			HttpSession session) throws Exception {
	
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
					
		System.out.println(activityService.getAll().size());
		return modelMap;
	}
	
	
	@RequestMapping(value="jsp/activity/saveLearningActivityAttributes.action")
	public @ResponseBody Map saveLearningActivityAttributes(
			@RequestParam(value="chk-active", required=false) String visible,
			@RequestParam(value="description", required=false) String description,
			@RequestParam(value="descriptionObjectives", required=false) String descriptionObjectives,
			@RequestParam(value="descriptionPrerequisites", required=false) String descriptionPrerequisites,
			@RequestParam(value="effort11", required=false) int effort11,
			@RequestParam(value="manager", required=false) String manager,
			@RequestParam(value="datefield1", required=false) String datefield,
			@RequestParam(value="timefield1", required=false) String timefield,
			HttpSession session) throws Exception {
	
		FinishingActivity finishingActivity = FinishingActivity.valueOf(effort11);
		
		String timelimit = "";
		//P1Y111M10DT10H10M10S
		
		String activityId = (String) session.getAttribute("graphId");
		String[] activityType = activityId.split(":");
		LearningActivity activity = (LearningActivity) activityService.getbyGraphId(activityType[1]);
		((LearningActivity) activity).setDescriptionObjective(descriptionObjectives);
		((LearningActivity) activity).setDescriptionPrerequisites(descriptionPrerequisites);
		activity.setFinishingActivity(finishingActivity);
		boolean vis = false;
		if(visible!=null){
			vis=true;
		}else{
			vis=false;
		}
		if(!StringUtils.isEmpty(manager)){
			activity.setAuthor(roleService.getRoleByName(manager));
		}
		
		activity.setVisible(vis);
		activity.setDescription(description);
		if(finishingActivity.equals(FinishingActivity.TIMELIMIT)){
			timelimit = getTimeLimit(timefield, datefield, timelimit);
			activity.setTimeLimit(timelimit);
		}
		activityService.saveOrUpdate(activity);
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
					
		modelMap.put("total", 1);
		modelMap.put("activity", activity);
		modelMap.put("success", true);
		return modelMap;
	}
	
	
	
	
	@RequestMapping(value="jsp/activity/saveSupportActivityAttributes.action")
	public @ResponseBody Map saveSupportActivityAttributes(
			@RequestParam(value="chk-active", required=false) String visible,
			@RequestParam(value="description", required=false) String description,
			@RequestParam(value="effort11", required=false) int effort11,
			@RequestParam(value="manager1", required=false) String manager,
			@RequestParam(value="datefield1", required=false) String datefield,
			@RequestParam(value="timefield1", required=false) String timefield,
			HttpSession session) throws Exception {
	
		FinishingActivity finishingActivity = FinishingActivity.valueOf(effort11);
		
		String timelimit = "";
		//P1Y111M10DT10H10M10S
		
		
		String activityId = (String) session.getAttribute("graphId");
		String[] activityType = activityId.split(":");
		SupportActivity activity = (SupportActivity) activityService.getbyGraphId(activityType[1]);
		activity.setFinishingActivity(finishingActivity);
		activity.setDescription(description);
		boolean vis = false;
		if(visible!=null){
			vis=true;
		}else{
			vis=false;
		}
		if(!StringUtils.isEmpty(manager)){
			activity.setAuthor(roleService.getRoleByName(manager));
		}
		
		activity.setVisible(vis);
		if(finishingActivity.equals(FinishingActivity.TIMELIMIT)){
			timelimit = getTimeLimit(timefield, datefield, timelimit);
			activity.setTimeLimit(timelimit);
		}
		
		activityService.saveOrUpdate(activity);
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
					
		modelMap.put("total", 1);
		modelMap.put("activity", activity);
		modelMap.put("success", true);
		return modelMap;
	}


	private String getTimeLimit(String timefield, String datefield, String timelimit) {
		if(!"".equals(datefield)){
			String[] date = datefield.split("/");
			timelimit+="P"+date[2]+"Y";
			timelimit+=date[1]+"M";
			timelimit+=date[0]+"DT";
		}
		if(!"".equals(timefield)){
			String[] time = timefield.split(":");
			timelimit+=time[0]+"H";
			timelimit+=time[1]+"M";
			timelimit+="0S";
		}
		
		return timelimit;
	}
	
	@RequestMapping(value="jsp/act/suportActivityStoreGrid.action")
	public @ResponseBody Map<String, Object> smGridView(
			HttpSession session,
			@RequestParam(value="limit",required=false) Integer limit,
			@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="start",required=false) Integer start,
			@RequestParam(value="dataFromGrid",required=false) List<String> dataFromGrid) throws Exception {

		String activityId = (String) session.getAttribute("graphId");
		System.out.println("activityId"+ activityId);
		String[] activityType = activityId.split(":");
		SupportActivity supportActivity = (SupportActivity) activityService.getbyGraphId(activityType[1]);
		int unitOfLearningId = (Integer)session.getAttribute("unitOfLearningId");
		
		List<Role> roles = roleService.getRoleListByUoL(1000, 1, 0, unitOfLearningId);
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		List<ActivityRolesDTO> rolesDTO = new ArrayList<ActivityRolesDTO>();
		if(dataFromGrid==null){
			ActivityRolesDTO activityRolesDTO = new ActivityRolesDTO(supportActivity,roles);
			supportActivity = (SupportActivity) activityService.save(supportActivity);
			modelMap.put("uolId", (Integer)session.getAttribute("unitOfLearningId"));
			modelMap.put("items", activityRolesDTO.getRolesDTO());
			modelMap.put("total", activityRolesDTO.getRolesDTO().size());
			modelMap.put("success", true);
		}else{
			int listSize = dataFromGrid.size();
			for(int i=0; i<listSize;){
				ActivityRolesDTO activityRolesDTO = new ActivityRolesDTO();
				activityRolesDTO.setRoleName(dataFromGrid.get(i++));
				activityRolesDTO.setSupportedRole(Boolean.valueOf(dataFromGrid.get(i++)));
				rolesDTO.add(activityRolesDTO);
			}
			List<Role> recipients = new ArrayList<Role>();
			for(ActivityRolesDTO dto : rolesDTO){
				Role role = roleService.getRoleByUoFandRoleName(dto.getRoleName(),(Integer)session.getAttribute("unitOfLearningId"));
				if(dto.isSupportedRole()){
					recipients.add(role);
				}
			}
			supportActivity.setSupportedRoles(recipients);
			activityService.save(supportActivity);
		}
		
		return modelMap;
	}


	public IActivityService getActivityService() {
		return activityService;
	}

	@Resource(name="activityService")
	public void setActivityService(IActivityService activityService) {
		this.activityService = activityService;
	}
	public IRoleService getRoleService() {
		return roleService;
	}
	@Resource(name="roleService")
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}


}
